package com.fzshang.shopping.api.controller;


import cn.hutool.core.util.StrUtil;
import com.alibaba.fastjson.JSONObject;
import com.fzshang.shopping.commen.bean.Comment;
import com.fzshang.shopping.commen.bean.SizeClassification;
import com.fzshang.shopping.commen.bean.layuiBean.ResultPage;
import com.fzshang.shopping.commen.bean.layuiBean.VoPage;
import com.fzshang.shopping.commen.controller.BaseController;
import com.fzshang.shopping.commen.entity.ApiResult;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;

import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

import java.util.List;

/**
 * <p>
 * 规格(大小) 前端控制器
 * </p>
 *
 * @author 冯政尚
 * @since 2020-03-15
 */
@Controller
@RequestMapping("admin/sizeClassification")
public class SizeClassificationController extends BaseController {

    @RequestMapping(value = "sizeClassification.html")
    public String sizeClassification(Model model){
        model.addAttribute("commodity",commodityClassificationService.list());
        return "admin/sizeClassification/sizeClassification";
    }
    @RequestMapping(value = "sizeClassification_add.html")
    public String sizeClassification_add(Model model){
        model.addAttribute("commodity",commodityClassificationService.list());
        return "admin/sizeClassification/sizeClassification_add";
    }


    /**
     * @param voPage
     * @return 后台查询
     */
    @RequestMapping(value = "selectMyPage",method = RequestMethod.GET)
    @ResponseBody
    public ResultPage<SizeClassification> selectMyPage(VoPage voPage, SizeClassification sizeClassification){
        return sizeClassificationService.selectMyPage(voPage,sizeClassification);
    }


    /**
     * @param sizeClassification
     * @param prods
     * @return 添加
     */
    @RequestMapping(value = "save")
    @ResponseBody
    public ApiResult save(SizeClassification sizeClassification,String prods){
        //去除空的  防止json转换失败
        prods= StrUtil.removeAll(prods,"[],");
        prods= StrUtil.removeAll(prods,",[]");
        List<SizeClassification> sizeClassifications = JSONObject.parseArray(prods,SizeClassification.class);
        for (int i = 0; i < sizeClassifications.size(); i++) {
            sizeClassifications.get(i).setClassificationId(sizeClassification.getClassificationId());
            sizeClassifications.get(i).setCommodityId(sizeClassification.getCommodityId());
            sizeClassifications.get(i).setCreateTime(System.currentTimeMillis());
        }
        if(sizeClassificationService.saveBatch(sizeClassifications)){
            return ApiResult.ok();
        }
        return ApiResult.fail();
    }

    /**
     * @param id
     * @return 删除
     */
    @RequestMapping(value = "deleteById")
    @ResponseBody
    public ApiResult  deleteById(Long id){
        if(sizeClassificationService.removeById(id)){
            return ApiResult.ok();
        }
        return ApiResult.fail();
    }



}
